package com.example.administrator.printoutindemo.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.administrator.printoutindemo.OutStoreLocationActivity;
import com.example.administrator.printoutindemo.R;
import com.example.administrator.printoutindemo.bean.OutKuListBean;
import com.example.administrator.printoutindemo.http.SPUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/9/11.
 */

public class OutStoreAdapter extends BaseAdapter {

    Context context;
    List<OutKuListBean> mlist = new ArrayList<>();
    LayoutInflater inflater;

    public OutStoreAdapter(Context context, List<OutKuListBean> mlist) {
        this.context = context;
        this.mlist = mlist;
        this.inflater = LayoutInflater.from(context);
    }


    public void shua() {
        if (mlist == null)
            mlist = new ArrayList<>();
        notifyDataSetInvalidated();
    }

    @Override
    public int getCount() {
        return mlist.size();
    }

    @Override
    public Object getItem(int i) {
        return mlist.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        Viewholder holder = null;
        if (view == null) {
            view = getInflater().inflate(R.layout.item_outku, null);
            holder = new Viewholder();
            holder.tv_wuliao = view.findViewById(R.id.tv_wuliao);
            holder.tv_mu_di_xiang_mu = view.findViewById(R.id.tv_mu_di_xiang_mu);
            holder.tv_xiangmu = view.findViewById(R.id.tv_xiangmu);
            holder.tv_ckdh = view.findViewById(R.id.tv_ckdh);
            holder.tv_lqr = view.findViewById(R.id.tv_lqr);
            holder.tv_sjhm = view.findViewById(R.id.tv_sjhm);
            holder.ll_item = view.findViewById(R.id.ll_item);
            holder.tv_cwmc = view.findViewById(R.id.tv_cwmc);
            holder.tv_ckmc = view.findViewById(R.id.tv_ckmc);
            holder.tv_gong_ying_shang = view.findViewById(R.id.tv_gong_ying_shang);
            view.setTag(holder);
        } else {
            holder = (Viewholder) view.getTag();
        }
        if (mlist.get(i).getMaterial() != null) {
            holder.tv_wuliao.setText(String.valueOf(mlist.get(i).getMaterial()));
        }
        if (mlist.get(i).getProjectname() != null) {
            holder.tv_xiangmu.setText(mlist.get(i).getProjectname());
        }
        if (mlist.get(i).getTarget_project() != null) {
            holder.tv_mu_di_xiang_mu.setText(mlist.get(i).getTarget_project());
        }
        if (mlist.get(i).getOut_storagelist_batchid() != null) {
            holder.tv_ckdh.setText(mlist.get(i).getOut_storagelist_batchid());
        }
        if (mlist.get(i).getReceive_userName() != null) {
            holder.tv_lqr.setText(mlist.get(i).getReceive_userName());
        }
        if (mlist.get(i).getReceive_userPhone() != null) {
            holder.tv_sjhm.setText(mlist.get(i).getReceive_userPhone());
        }
        if (mlist.get(i).getStorename() != null) {
            holder.tv_ckmc.setText(mlist.get(i).getStorename());
        }
        if (mlist.get(i).getSpacename() != null) {
            holder.tv_cwmc.setText(mlist.get(i).getSpacename());
        }

        holder.ll_item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SPUtil.putString(context, "batchid", mlist.get(i).getOut_storagelist_batchid());
                Intent intent = new Intent(context, OutStoreLocationActivity.class);
                context.startActivity(intent);
            }
        });
        return view;
    }

    public LayoutInflater getInflater() {
        return inflater;
    }

    public void setInflater(LayoutInflater inflater) {
        this.inflater = inflater;
    }

    public void addAll(List<OutKuListBean> cklb) {
//        mlist.clear();
        mlist.addAll(cklb);
        notifyDataSetChanged();
    }

    public void clearData() {
        mlist.clear();
    }

    class Viewholder {

        TextView tv_wuliao, tv_xiangmu, tv_ckdh, tv_lqr, tv_sjhm, tv_cwmc, tv_ckmc, tv_mu_di_xiang_mu, tv_gong_ying_shang;
        LinearLayout ll_item;
    }
}
